package sg.edu.ntu.metaweb.http;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import sg.edu.ntu.metaweb.pastry.PastryNodeHandle;

public class HTTPServer {

	ServerSocket serverSocket = null;
	Socket socket = null;
	
	PastryNodeHandle pastryNodeHandle;	
	
	
	public HTTPServer(int port, PastryNodeHandle pastryNodeHandle) {
		
		this.pastryNodeHandle = pastryNodeHandle;
		
		try {
		    serverSocket = new ServerSocket(port);
		    System.out.println("[Start listening]");
		    while(true) {
		        socket = serverSocket.accept();
		        RequestHandler handler = new RequestHandler(socket, this.pastryNodeHandle);
		        handler.start();
		    }
		} catch(IOException ioe) {
		    ioe.printStackTrace();
		} finally {
		    try {
		        serverSocket.close();
		    } catch(IOException ioe) {
		    	ioe.printStackTrace();
		    }
		}
	}


}
